package com.saifan.wyy_ov.ui.onlishop;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.data.bean.GroupBuyBean;
import com.saifan.wyy_ov.data.bean.GroupBuyBean.GoodsList;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.utils.ImgUtil;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;

public class AdapterGroupBuyItem extends BaseAdapter {
    Context context;
    List<GoodsList> list;
    GroupBuyBean groupBuyBean;
    int showCount = 2;

    public int getShowCount() {
        return showCount;
    }

    public void setShowCount(int showCount) {
        this.showCount = showCount;
        notifyDataSetChanged();
    }

    public AdapterGroupBuyItem(Context context, GroupBuyBean groupBuyBean, List<GoodsList> list) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.list = list;
        this.groupBuyBean = groupBuyBean;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if ( list!= null&&!list.isEmpty() ) {
            if (list.size() < showCount) {
                return list.size();
            }
            return showCount;
        } else
            return 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        if ( list!= null&&!list.isEmpty() )
            return list.get(position);
        else
            return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GoodsList goodsList = list.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.group_buy_goods_item, null);
            viewHolder.goods_img = (ImageView) convertView.findViewById(R.id.goods_img);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tv_price2 = (TextView) convertView.findViewById(R.id.tv_price2);
            viewHolder.tv_odd = (TextView) convertView.findViewById(R.id.tv_odd);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String startTime = goodsList.getGroupBuyingStartTime().substring(0, 10) + " " + goodsList.getGroupBuyingStartTime().substring(11, 16);
        String endTime = goodsList.getGroupBuyingEndTime().substring(0, 10) + " " + goodsList.getGroupBuyingEndTime().substring(11, 16);
        ImgUtil.DisPlayImg(goodsList.getCoverPhoto(), viewHolder.goods_img, context);
        viewHolder.tv_goods_name.setText(StringUtil.returnShow(goodsList.getGoodsName()));
        viewHolder.tv_time.setText(startTime + "至" + endTime);
        viewHolder.tv_price.setText(context.getString(R.string.coin) + PriceUtils.getPrice(goodsList.getGoodsPrice()));
        viewHolder.tv_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        viewHolder.tv_price2.setText(context.getString(R.string.coin) + PriceUtils.getPrice(goodsList.getGroupBuyingPrice()));
        viewHolder.tv_odd.setText(goodsList.getGroupBuyingRemainingCount() + " ");

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {


                CommonDataBean submitOrderRequst = new CommonDataBean();
                submitOrderRequst.setID(goodsList.getID());
                submitOrderRequst.setStoreId(groupBuyBean.getID() + "");
                GetData getDataSubmit = new GetData();
                getDataSubmit.getDataJson(context, Api.GroupBuy.GOODSDETAIL, submitOrderRequst, "请稍候!", new ResponseData() {

                    @Override
                    public void onSuccess(String json) {
                        Intent intent = new Intent(context, GroupBuyDetailsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("goods", goodsList);
                        bundle.putSerializable("groupBuyBean", groupBuyBean);
                        intent.putExtra("json", json);
                        intent.putExtras(bundle);
                        context.startActivity(intent);

                    }

                    @Override
                    public void onFailed(String str) {


                    }
                });


            }
        });
        return convertView;
    }

    static class ViewHolder {
        public ImageView goods_img;
        public TextView tv_goods_name;
        public TextView tv_time;
        public TextView tv_price;
        public TextView tv_price2;
        public TextView tv_odd;

    }
}
